package bme.andraskindler.treenventory.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import bme.andraskindler.treenventory.R;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

/**
 * @author andraskindler
 * 
 * 
 *         Ez az activity a beállítások megjelenítéséért felel. Az alkalmazás SharedPreferences-ben tárolja az opciókat.
 */
public class OptionsActivity extends SherlockActivity {

	public final static String SETTINGS = "settings";
	public final static String SETTINGS_RESOLUTION = "settings.resolution";
	public final static String SETTINGS_ACCURACY = "settings.accuracy";
	public final static String SETTINGS_HEIGHT = "settings.height";
	public final static String SETTINGS_IP = "settings.ip";
	public final static String SETTINGS_CORRECTION = "settings.correction";

	public final static float SETTINGS_TRESHOLD = 2.5f;

	private RadioGroup mRadioGroupAccuracy, mRadioGroupResolution;
	private EditText mEditTextIP;
	private CheckBox mCheckBoxCorrection;

	private int height;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final View root = getLayoutInflater().inflate(R.layout.activity_options, null);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);

		final SharedPreferences preferences = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE);

		mRadioGroupResolution = RadioGroup.class.cast(root.findViewById(R.id.options_radio_resolution));
		final int resolution = preferences.getInt(SETTINGS_RESOLUTION, 1);
		switch (resolution) {
		case 0:
			mRadioGroupResolution.check(R.id.options_radio_resolution_low);
			break;
		case 1:
			mRadioGroupResolution.check(R.id.options_radio_resolution_mid);
			break;
		default:
			mRadioGroupResolution.check(R.id.options_radio_resolution_hi);
			break;
		}

		mCheckBoxCorrection = CheckBox.class.cast(root.findViewById(R.id.cb_options_correction));
		mCheckBoxCorrection.setChecked(preferences.getBoolean(SETTINGS_CORRECTION, true));

		mRadioGroupAccuracy = RadioGroup.class.cast(root.findViewById(R.id.options_radio_accuracy));
		final int accuracy = preferences.getInt(SETTINGS_ACCURACY, 1);
		switch (accuracy) {
		case 1:
			mRadioGroupAccuracy.check(R.id.options_radio_accuracy_one);
			break;
		case 3:
			mRadioGroupAccuracy.check(R.id.options_radio_accuracy_two);
			break;
		case 5:
			mRadioGroupAccuracy.check(R.id.options_radio_accuracy_three);
			break;
		default:
			break;
		}

		final TextView height_text = TextView.class.cast(root.findViewById(R.id.options_height_text));
		height = preferences.getInt(SETTINGS_HEIGHT, 25);
		height_text.setText(height + "%");
		final SeekBar heightbar = SeekBar.class.cast(root.findViewById(R.id.options_seekbar));
		heightbar.setProgress(height);

		heightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				height = progress;
				height_text.setText(progress + "%");
			}
		});

		final View.OnClickListener listener = new View.OnClickListener() {

			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.options_save:
					save();
					break;
				case R.id.options_reset:
					mRadioGroupResolution.check(R.id.options_radio_resolution_mid);
					mRadioGroupAccuracy.check(R.id.options_radio_accuracy_one);
					heightbar.setProgress(25);
					save();
					break;
				default:
					break;
				}
				finish();

			}
		};

		Button.class.cast(root.findViewById(R.id.options_cancel)).setOnClickListener(listener);
		Button.class.cast(root.findViewById(R.id.options_save)).setOnClickListener(listener);
		Button.class.cast(root.findViewById(R.id.options_reset)).setOnClickListener(listener);

		mEditTextIP = EditText.class.cast(root.findViewById(R.id.et_options_ip));
		mEditTextIP.setText(preferences.getString(SETTINGS_IP, "IP cím"));
		setContentView(root);
	}

	/**
	 * Ez a függvény felel a mentésért.
	 */
	private void save() {
		final Editor editor = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit();
		editor.putInt(SETTINGS_HEIGHT, height);

		switch (mRadioGroupAccuracy.getCheckedRadioButtonId()) {
		case R.id.options_radio_accuracy_one:
			editor.putInt(SETTINGS_ACCURACY, 1);
			break;
		case R.id.options_radio_accuracy_two:
			editor.putInt(SETTINGS_ACCURACY, 3);
			break;
		default:
			editor.putInt(SETTINGS_ACCURACY, 5);
			break;
		}

		switch (mRadioGroupResolution.getCheckedRadioButtonId()) {
		case R.id.options_radio_resolution_low:
			editor.putInt(SETTINGS_RESOLUTION, 0);
			break;
		case R.id.options_radio_resolution_mid:
			editor.putInt(SETTINGS_RESOLUTION, 1);
			break;
		default:
			editor.putInt(SETTINGS_RESOLUTION, 2);
			break;
		}

		editor.putBoolean(SETTINGS_CORRECTION, mCheckBoxCorrection.isChecked());

		editor.putString(SETTINGS_IP, mEditTextIP.getText().toString());

		editor.commit();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		default:
			break;
		}
		return true;
	}

}
